<?php

function curl_Get($url, $jpost) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

function curl_Post($url, $jpost) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jpost);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

function startQuiz($class_uid, $quiz_uid) {
	$url = "http://146.95.30.80:9998/class/$class_uid/quiz/$quiz_uid/start";
	$jpost = '';
	$result = curl_Post($url, $jpost);
	return $result;
}

function stopQuiz($class_uid, $quiz_uid) {
	$url = "http://146.95.30.80:9998/class/$class_uid/quiz/$quiz_uid/stop";
	$jpost = '';
	$result = curl_Post($url, $jpost);
	return $result;
}

function quizstat($class_uid, $quiz_uid) {
	$url = "http://146.95.30.80:9998/class/$class_uid/quiz/$quiz_uid/stats";
	$jpost = '';
	$result = curl_Get($url, $jpost);
	return $result;
}

function addStudent() {
	$url = 'http://146.95.30.80:9998/student';
	extract($_POST);
	$post["gender"] = $gender;
	$post["email"] = $email;
	$post["first_name"] = $fname;
	$post["last_name"] = $lname;

	$jpost = json_encode($post);
	$result = curl_Post($url, $jpost);
	return $result;
}

function addQuiz() {
	$url = 'http://146.95.30.80:9998/quiz';
	extract($_POST);

	if ($B != NULL) {
		$type = "mc";
		$answer[] = $CAC;
		$choices[] = $A;
		$choices[] = $B;
	
		if ($C != NULL) {
			$choices[] = $C;
		}

		if ($D != NULL) {
			$choices[] = $D;
		}

		if ($E != NULL) {
			$choices[] = $E;
		}

		if ($F != NULL) {
			$choices[] = $F;
		}
	}

	else {
		$type = "sa";
		$choices[] = $CA;
		$answer[] = 0;
	}

	$post["topic"] = $topic;
	$post["type"] = $type;
	$post["question"] = $quest;
	$post["choices"] = $choices;
	$post["answers"] = $answer;

	$jpost = json_encode($post);

	$result = curl_Post($url, $jpost);
	print "$result\n";
	
	return $jpost;
}

function amend() {
        global $r_a;
        global $url;
        if($r_a["type"] == "MC") {
                $url .= "/choices";
        }
        else {
                print $r_a["type"];
        }
}

?>
